package com.earnings.okhttputils.utils.https;

import android.content.Context;

import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.UrlConstant.HttpUrl;
import com.earnings.okhttputils.utils.User;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by HOHO on 2017/5/2.
 * 通用访问接口
 */

public class CommonAccess<T> {
    private Context context;
    private String nickname, sex, headurl;
    private File imgfile;
    private HttpObjectCallback<T> callback;
    private HttpArrayCallback<T> callbacks;
    private User user;

    public CommonAccess(final Context context) {
        this.context = context;
       // user= ((BaseActivity)context).app.getUser();
        if(user!=null){
        nickname=user.getNickname();
        setSex(user.getSex());
        headurl=user.getHead_pic();}
    }

    /*
                更新用户资料
                 */
    public void userUpdateData() {
        Map map = new HttpMap(context);

        if(imgfile!=null){

            Map<String,File> filemap = new HashMap<String,File>( );
            filemap.put( imgfile.getName(),imgfile );
                OkHttpUtils.post().files( "head_pic" ,filemap).url( HttpUrl.USER_UPDATE_URL ).params( map ).build().execute( callback );

        }else {
            if(nickname!=null)
                map.put( "nickname", nickname );
                if(headurl!=null)
                map.put( "head_pic", headurl );
            if(sex!=null)
                map.put( "sex", sex );

                OkHttpUtils.post().url( HttpUrl.USER_UPDATE_URL ).params( map ).build().execute( callback );

        }
    }

    /*
                获取用户资料
                 */
    public void userLoadData() {
        Map map = new HttpMap(context );

            OkHttpUtils.post().url( HttpUrl.USER_LOAD_URL ).params(map).build().execute( callback );

    }
    /*
                   获取用户余额信息
                    */
    public void userLoadBalance() {
        Map map = new HttpMap(context );

        OkHttpUtils.post().url( HttpUrl.USER_TREASURE_URL ).params(map).build().execute( callback );

    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public void setSex(String sex) {
        if (sex.equals( "男" )) sex = "1";
        else if (sex.equals( "女" )) sex = "2";
        else sex = "0";
        this.sex = sex;
    }

    /*
               上传图片
                */
    public void uploadImg(File file) {
        Map map = new HttpMap(context);

        if(file!=null){

            Map<String,File> filemap = new HashMap<String,File>( );
            filemap.put( file.getName(),file );
            OkHttpUtils.post().files( "type" ,filemap).url( HttpUrl.IMG_UPLOAD ).params( map ).build().execute( callbacks );
        }
    }


    public void setHeadurl(String headurl) {
        this.headurl = headurl;
    }
    public void setHeadFile(File file) {imgfile=file;}

    public void setCallback(HttpObjectCallback<T> callback) {
        this.callback = callback;
    } public void setCallbacks(HttpArrayCallback<T> callbacks) {
        this.callbacks = callbacks;
    }
}
